<?php

namespace zhtfrontend\modules\teachergrowth\controllers;

use common\components\helpers\Fun;
use common\models\datas\Student;
use doumiaoduo\aliyun\OssuploaderHelper;
use yii\console\Exception;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use yii\web\UploadedFile;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\ClassLesson;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageAttribute;
use zhtfrontend\models\datas\ClassManageTeacher;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\ClassStudent;
use zhtfrontend\models\datas\Comment;
use zhtfrontend\models\datas\CommentRelation;
use zhtfrontend\models\datas\LessonStudents;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\StudentCheck;
use zhtfrontend\models\datas\Teacher;
use zhtfrontend\modules\teachergrowth\components\rest\ActiveController;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class EvaluateController extends ActiveController

{
    private $_config;
    public $modelClass = '';

    public $serializer = [
        'class' => 'zhtfrontend\modules\teachergrowth\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }


    //下面是首页
    public function actionTeacherlists(){
        $data = \Yii::$app->request->post();
        $user_id=ArrayHelper::keyExists('id', $data) ? $data['id'] : '';
        $role=ArrayHelper::keyExists('role', $data) ? $data['role'] : '';
        if (empty($user_id)){
            return ['statusCode' =>0, 'message' => 'error', 'result' => '用户不存在'];
        }else{
            $mechanic_teacher_array=[];
            if($role=="class_director"){
                //查找老师负责的机构信息
                $mechanic=Mechanic::find()->select('id')->where(['admin_id'=>$user_id])->column();
                $mechanic_teacher=Teacher::find()->where(['mechanic_id'=>$mechanic])->all();
                $mechanic_teacher_array=[];
                foreach ($mechanic_teacher as $key=>$val){
                    $comment_info=Comment::find()->where(['valuator_type'=>3,'valuator_id'=>$user_id,'accepter_type'=>2,'accepter_id'=>$val->id])->one();
                    if (isset($comment_info)){
                        $comment_info_score=CommentRelation::find()->where(['comment_id'=>$comment_info->id])->one();
                    }
                    $sexiconBox=($val->sex==0)?'dmd-female-icon':'dmd-male-icon';
                    $sexIcon=($val->sex==0)?'icon-female':'icon-male';
                    $mechanic_name=Mechanic::findOne($val->mechanic_id);
                    $mechanic_teacher_array[$key]=[
                        'id'=> $val->id,
                        'sex'=>$val->sex,
                        'src'=>$val->img,
                        'teacherName'=>$val->name,
                        'sexiconBox'=>$sexiconBox,
                        'sexIcon'=>$sexIcon,
                        'teacherState'=>isset($comment_info)?'已考评':'需考评',
                        'teacherScore'=>isset($comment_info_score)?$comment_info_score->for_value:0,
                        'institution'=>isset($mechanic_name)?$mechanic_name->name:'',
                    ];
                }
            }else if($role=="teache_teacher"){
                //首先查找当前教师属于哪个机构，如果是教师传过来的就是教师id
                $teacher_info=Teacher::findOne($user_id);
                $mechanic_info=Mechanic::findOne($teacher_info->mechanic_id);
                $admin_info=Admin::find()->where(['id'=>$mechanic_info->admin_id])->all();
                foreach ($admin_info as $key=>$value){
                    $comment_info=Comment::find()->where(['valuator_type'=>2,'valuator_id'=>$user_id,'accepter_type'=>3,'accepter_id'=>$value->id])->one();
                    if (isset($comment_info)){
                        $comment_info_score=CommentRelation::find()->where(['comment_id'=>$comment_info->id])->one();
                    }
                    $mechanic_teacher_array[$key]=[
                        'id'=> $value->id,
                        'sex'=>1,
                        'src'=>'',
                        'teacherName'=>$value->name,
                        'sexiconBox'=>'dmd-male-icon',
                        'sexIcon'=>'icon-male',
                        'teacherState'=>isset($comment_info)?'已考评':'需考评',
                        'teacherScore'=>isset($comment_info_score)?$comment_info_score->for_value:0,
                        'institution'=>'机构负责人',
                    ];
                }
            }
            $items=[
                'mechanic_teacher_array'=>$mechanic_teacher_array,
                'teacherNum'=>count($mechanic_teacher_array),
            ];
            return ['statusCode' => 200, 'message' => 'success', 'items' => $items];
        }
    }

    //下面是教师端的评价，后期需要进一步修改，也就是评价的条目需要从数据库里面读取
    public function actionEvaluatelist(){
        $data = \Yii::$app->request->post();
        $role=ArrayHelper::keyExists('role', $data) ? $data['role'] : '';
        $evaluate_id=ArrayHelper::keyExists('evaluate_id', $data) ? $data['evaluate_id'] : '';//评价者id
        $accepter_id=ArrayHelper::keyExists('accepter_id', $data) ? $data['accepter_id'] : '';//被评价者id
        $current_item=ArrayHelper::keyExists('current_item', $data) ? $data['current_item'] : '';//当前评价的是多少
        //如果当前角色是班主任得话，就需要查找需要评价的老师的条目
        if (empty($evaluate_id) || empty($accepter_id) || empty($role)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '信息不存在'];
        }else{
            if ($role=="class_director"){//如果登录的是班主任
                $teacher_info=(new Query())->select('*')->from('{{%teacher}}')->where(['id'=>$accepter_id])->one(Fun::getSiteDb());//需要查找老师的信息
                $comments_info_teacher=(new Query())->select("*")->from('{{%comments_info}}')->where(['valuator_type'=>3,'accepter_type'=>2,'status'=>1])->all(Fun::getSiteDb());
            }
        }
        return ['statusCode' => 200, 'message' => 'success', 'items' => ''];
    }






   //下面是评价保存
    public function actionEvaluates(){
        $data = \Yii::$app->request->post();
        $role=ArrayHelper::keyExists('role', $data) ? $data['role'] : '';
        $evaluate_id=ArrayHelper::keyExists('evaluate_id', $data) ? $data['evaluate_id'] : '';
        $accepter_id=ArrayHelper::keyExists('accepter_id', $data) ? $data['accepter_id'] : '';
        $current_item=ArrayHelper::keyExists('current_item', $data) ? $data['current_item'] : '';
        if(empty($evaluate_id) || empty($accepter_id)){
            return ['statusCode' =>0, 'message' => 'error', 'result' => '用户不存在'];
        }else{
            if($role=="class_director"){
                //如果是机构负责人评价老师
                $comment_info=Comment::find()->where(['valuator_type'=>3,'valuator_id'=>$evaluate_id,'accepter_type'=>2,'accepter_id'=>$accepter_id])->one();
                if (isset($comment_info)){
                    $comment=$comment_info;
                }else{
                    $comment=new Comment();
                }
                $comment->title='机构负责人评价老师';
                $comment->site_id=2;
                $comment->description='该员工的工作态度及能力';
                $comment->valuator_type=3;
                $comment->valuator_id=$evaluate_id;
                $comment->accepter_type=2;
                $comment->accepter_id=$accepter_id;
                $comment->created_at=time();
                $comment->state=1;
                if ($comment->save()){
                    $comment_relations=CommentRelation::find()->where(['comment_id'=>$comment->id])->one();
                    if (isset($comment_relations)){
                        $comment_relation=$comment_relations;
                    }else{
                        $comment_relation=new CommentRelation();
                    }
                    $comment_relation->comment_id=$comment->id;
                    $comment_relation->comment_info_id=$comment->id;
                    $comment_relation->for_value=$current_item;
                    $comment_relation->save();
                }
                return ['statusCode' =>200, 'message' => 'error', 'items' => '保存成功'];
            }else{
                //如果是老师负责人评价机构
                $comment_info=Comment::find()->where(['valuator_type'=>2,'valuator_id'=>$evaluate_id,'accepter_type'=>3,'accepter_id'=>$accepter_id])->one();
                if (isset($comment_info)){
                    $comment=$comment_info;
                }else{
                    $comment=new Comment();
                }
                $comment->title='老师评价机构负责人';
                $comment->site_id=2;
                $comment->description='该员工的工作态度及能力';
                $comment->valuator_type=2;
                $comment->valuator_id=$evaluate_id;
                $comment->accepter_type=3;
                $comment->accepter_id=$accepter_id;
                $comment->created_at=time();
                $comment->state=1;
                if ($comment->save()){
                    $comment_relations=CommentRelation::find()->where(['comment_id'=>$comment->id])->one();
                    if (isset($comment_relations)){
                        $comment_relation=$comment_relations;
                    }else{
                        $comment_relation=new CommentRelation();
                    }
                    $comment_relation->comment_id=$comment->id;
                    $comment_relation->for_value=$current_item;
                    $comment_relation->save();
                }
                return ['statusCode' =>200, 'message' => 'error', 'items' => '保存成功'];
            }
        }
    }

}